<%--
  Created by IntelliJ IDEA.
  User: Ken
  Date: 2022/4/27
  Time: 15:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>忘记密码</title>
    <style>
        * {
            padding: 0px;
            margin: 0px;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .main_div {
            width: 400px;
            height: 300px;
            border: 1px solid black;
            border-radius: 10px;
            box-shadow: 10px 10px 10px gray;
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            align-items: center;
        }

        .title_div {
            font-size: 30px;
            font-family: "楷体";
            text-shadow: 10px 10px 10px gray;
        }

        .btn_div button{
            width: 80px;
        }
    </style>

    <script src="js/jquery.min.js"></script>
    <script>
        //发送找回密码的邮件
        function sendMail(){
            //1、获取填写的账号内容
            var username = $("[name='username']").val();
            if (username.trim() == "") {
                alert("账号不能为空！");
                return;
            }

            //2、发送ajax 请求Servlet 发送邮件
            $.ajax({
                type: "POST",
                url: "playerServlet?method=sendPasswordEmail",
                data: {
                    username: username
                },
                success: function(data){
                    if (data.status == -1) {
                        $("#msg").html("账号不存在！");
                    } else if(data.status == 1) {
                        //获取发送的邮箱地址
                        var email = data.email;
                        //1231231312@sina.com
                        var emailInfo = email.substring(3, email.indexOf("@"));
                        email = email.replace(emailInfo, "******");

                        //组装邮箱链接
                        var mailUrl = "http://mail." + email.substring(email.indexOf("@") + 1);

                        $("#msg").html("验证码已经成功发送到该邮箱：" + email + ", <br/><a href='" + mailUrl + "'>去到该邮箱</a>");
                    }
                },
                dataType: "json"
            });
        }
    </script>
</head>
<body>
<form action="playerServlet?method=updatePassword" method="post">
    <div class="main_div">
        <div class="title_div">忘记密码</div>
        <div id="msg"></div>
        <div>
            <div>账号：<input name="username"/><button type="button" onclick="sendMail();">发送邮件</button></div>
            <div>新的密码：<input name="newpassword" type="password"/></div>
            <div>验证码：<input name="code"/></div>
        </div>
        <div class="btn_div">
            <button type="submit">修改密码</button>
        </div>
    </div>
</form>
</body>
</html>